import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/lget.dart';
import 'package:liuxlcommon/view/extension_widget.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/manage/location_manage/location_manage.dart';
import 'package:love_newf/app/page/home/home_listener.dart';
import 'package:love_newf/app/page/home/home_logic.dart';
import 'package:love_newf/app/page/index/live_list/live_list_view.dart';
import 'package:love_newf/app/page/open_live_setting/open_live_setting_view.dart';
import 'package:love_newf/app/page/search/search_view.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

import 'index_logic.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage>
    with
        AutomaticKeepAliveClientMixin,
        HomeListener,
        SingleTickerProviderStateMixin {
  final logic = Get.put(IndexLogic());
  final GlobalKey<RefreshIndicatorState> _indicatorKey =
      GlobalKey<RefreshIndicatorState>();

  @override
  void initState() {
    super.initState();
    Get.find<HomeLogic>().mapHomeListener["index"] = this;
    onHomePageChanged(0);
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: logic.tabNames.length,
        child: LColumn.eazy([topBox, tabBox, listBox])
            .align(Alignment.topLeft)
            .bgColor(Colors.white));
  }

  get tabBox => SizedBox(
        width: 250.w,
        child: TabBar(
            dividerHeight: 0,
            labelPadding: EdgeInsets.zero,
            indicatorPadding: EdgeInsets.zero,
            padding: EdgeInsets.zero,
            isScrollable: false,
            indicatorColor: AppColors.primaryColor,
            labelStyle: TextStyle(
                color: 0xFF323232.color, fontSize: 20.w, fontWeight: fw6),
            unselectedLabelStyle:
                TextStyle(color: 0xFF868A98.color, fontSize: 16.w),
            tabs: [
              for (int i = 0; i < logic.tabNames.length; i++)
                Tab(text: logic.tabNames[i])
            ]),
      );

  get topBox => LRow.eazy([
        LRow.eazy([
          LImage.easy(Assets.iconSearch, 22.w, 22.w),
          LText.easy("请输入红娘/月老ID", 14.w, null, AppColors.textHintColor)
        ])
            .size(null, 36.w)
            .padding(left: 8.w)
            .radius(39.w)
            .bgColor(0xFFF5F4F7.color)
            .flex(1)
            .onTapL(() => Get.to(const SearchPage())),
        10.lsw,
        LImage.easy(Assets.homeOpenLive, 30.w, 66.w)
            .onTapL(() => Get.to(const OpenLiveSettingPage())),
      ]).padding(left: 11.w, right: 13.w, top: LGet.startBarHeight + 6.w);

  get listBox => LExpand(
          child: TabBarView(
        children: [
          for (int i = 0; i < logic.tabNames.length; i++) LiveListPage(i)
        ],
      ));

  @override
  bool get wantKeepAlive => true;

  @override
  void onHomePageChanged(position) {
    if (position != 0) return;
    //已经加载完成了， 下次进入做刷新
    if (logic.indexData != null) {
      _indicatorKey.currentState?.show();
    }
    if (LocationManage.instance.currLocInfo == null) {
      LocationManage.instance.determinePosition().then((value) {
        print(json.encode(value.toString()));
      });
    }
  }
}
